package leetcode.leetcode_49;

import java.util.*;

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        HashMap<String, List<String>> map = new HashMap<>();
        for (String str : strs){
            String sortedString = sortString(str);
            if (map.containsKey(sortedString)){
                List<String> stringList = map.get(sortedString);
                stringList.add(str);
                map.put(sortedString,stringList);
            }
            else {
                List<String> stringList = new ArrayList<>();
                stringList.add(str);
                map.put(sortedString,stringList);
            }
        }
        Collection<List<String>> values = map.values();
        return new ArrayList<>(values);
    }
    public String sortString(String str){
        char[] charArray = str.toCharArray();
        Arrays.sort(charArray);
        StringBuilder builder = new StringBuilder();
        for (char c : charArray){
            builder.append(c);
        }
        String s = builder.toString();
        return s;
    }
}